package top.kaoshanji.p1ch03b;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单-p1ch03b-01：客户端请求授权码
 * @author kaoshanji
 * ^_^
 * create time 2025/5/25 13:41
 */
@WebServlet(urlPatterns = {"/authorize"})
public class AuthorizeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        // 从客户端重定向的URL里获取参数
        String responseType = req.getParameter("response_type");
        String redirectUri =req.getParameter("redirect_uri");
        String clientId = req.getParameter("client_id");

        System.out.println("...客户端请求授权码参数...responseType:"+responseType+"...redirectUri:"+redirectUri+"...clientId:"+clientId);

        // 跳转到授权页面，然用户确认权限范围

        // 一路传递...
        req.setAttribute("response_type", responseType);
        req.setAttribute("redirect_uri", redirectUri);
        req.setAttribute("client_id", clientId);

        // 打开授权权限页面
        req.getRequestDispatcher("/approve.jsp").forward(req, resp);

    }

}
